<%-- 
    Document   : addProduct
    Created on : Jan 3, 2013, 4:03:08 PM
    Author     : berto
--%>

<%@page import="it.unitn.science.matr155863_148346_145963.bccastestore.items.Category"%>
<%@page import="it.unitn.science.matr155863_148346_145963.bccastestore.items.Image"%>
<%@page import="it.unitn.science.matr155863_148346_145963.bccastestore.DBManager"%>
<%@page import="it.unitn.science.matr155863_148346_145963.bccastestore.items.Product"%>
<%@page import="it.unitn.science.matr155863_148346_145963.bccastestore.items.UM"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
private DBManager db;
    private String srcCategory;
    private String srcUm;
    private String srcImage;
    
    @Override
    public void init() throws UnavailableException{
            db=(DBManager)super.getServletContext().getAttribute("db_manager");
            
            ArrayList<Category> listCat=db.getCategories();
            srcCategory="<select name=\""+Product.CATEGORY+"\">";
            for(Category c:listCat){
                srcCategory=srcCategory+"<option value=\""+c.getId()+"\" >"+c.getName()+"</option>";
            }
            srcCategory=srcCategory+"</select>";
            
            ArrayList<Image> listImage=db.getImages();
            srcImage="<select name=\""+Product.IMAGE+"\">"/*
                    + "<option value=\"-1\">Load from pc</option>"*/;
            for(Image i:listImage){
                 srcImage=srcImage+"<option value=\""+i.getId()+"\">"+i.getName()
                        +"</option>";
            }
            srcImage=srcImage+"</select>";
            
            ArrayList<UM> listUm=db.getUMs();
            srcUm="<select name=\""+Product.UM+"\">";
            for(UM um:listUm){
                srcUm=srcUm+"<option value=\""+um.getId()+"\" >"+um.getName()+"</option>";
            }
            srcUm=srcUm+"</select>";
    }
%>

<%
        String mess="";
        if(session.getAttribute("error_on")!=null){
            mess="Unable to add the product.<br/>"
                 + "You have an error on field:<br/>"
                 + (String)session.getAttribute("error_on") ;
            session.removeAttribute("error_on");
        }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add product</title>
    </head>
    <body>
        <div class="content">
        <h1>Add product:</h1>
        <p id="add_product_error_log"><%=mess%></p>
        <form name="form_add_product" method="post"
              enctype="multipart/form-data"  
                    action="<%=response.encodeURL(request.getContextPath()+"/services/addProduct")%>">
                        <table>
                            <tr>
                                <td>Name:</td>
                                <td>
                                    <input type=text 
                                    name="name"
                                    value="<%=request.getParameter("name")==null? "":request.getParameter("name")%>"
                                    />
                                </td>
                            </tr>
                            <tr>
                                <td>Quantity:</td>
                                <td>
                                    <input type=text 
                                    name="quantity"
                                    value="<%=request.getParameter("quantity")==null? "":request.getParameter("quantity")%>"
                                    />
                                    <%=srcUm%>
                                </td>
                            </tr>
                            <tr>
                                <td>Image:</td>
                                <td>
                                    <%=srcImage%>
                                    <input type="file" name="image_src" />
                                </td>
                            </tr>
                            <tr>
                                <td>Description:</td>
                                <td>
                                    <input type=text 
                                    name="description"
                                    value="<%=request.getParameter("description")==null? "":request.getParameter("description")%>"
                                    />
                                </td>
                            </tr>
                            <tr>
                                <td>Category:</td>
                                <td>
                                <%=srcCategory%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="reset"
                                    name="reset"  
                                    value="Reset"
                                    /> 
                                </td>
                                <td>
                                    <input type="button"
                                           onclick="ValidateProduct()"
                                    name="add_product"
                                    value="Add"
                                    />
                                </td>
                            </tr>
                        </table>
                    <script type="text/javascript" 
                        src="<%=request.getContextPath()%>/javascript/addProduct.js">
                    </script>
                    </form>
    </div>
    </body>
</html>
